package com.qianchen.chatbot.api.dto;

import java.io.*;
import java.net.URL;
import java.util.Properties;

public class StatusCodeConfig {
    private static StatusCodeConfig instance;
    private static Properties properties = new Properties();

    private StatusCodeConfig() {
        this.initDefault();
        this.initExtend();
    }

    private static synchronized void initInstance() {
        System.out.println("initInstance");
        if (instance == null) {
            System.out.println("instance==null");
            instance = new StatusCodeConfig();
        }

    }

    private void load(File file) {
        if (file != null && file.exists()) {
            try {
                this.load((InputStream)(new FileInputStream(file)));
            } catch (FileNotFoundException var3) {
                System.out.println("load.file happen error, file:" + (file == null ? "" : file.getName()));
                var3.printStackTrace();
            }

        }
    }

    private void load(InputStream input) {
        if (input != null) {
            BufferedReader br = null;

            try {
                System.out.println("load");
                br = new BufferedReader(new InputStreamReader(input, "UTF-8"));
                properties.load(br);
            } catch (IOException var16) {
                System.out.println("load.inputStream happen error:" + var16.getMessage());
                var16.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        br.close();
                    } catch (IOException var15) {
                        var15.printStackTrace();
                    }
                }

                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException var14) {
                        var14.printStackTrace();
                    }
                }

            }

        }
    }

    private void load(String filePath) {
        try {
            System.out.println("load");
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filePath);
            if (is == null) {
                System.out.println("状态码配置文件" + filePath + "加载失败，请确认文件是否存在");
                return;
            }

            this.load(is);
        } catch (Exception var3) {
            System.out.println("状态码配置文件" + filePath + "加载失败，请确认文件是否存在");
            var3.printStackTrace();
        }

    }

    private void initDefault() {
        String filePath = "com/qianchen/chatbot/api/dto/status-code-default.properties";
        this.load(filePath);
    }

    private void initExtend() {
        String dir = "/conf";
        URL path = StatusCodeConfig.class.getResource(dir);
        if (path == null) {
            System.out.println("don't find dir：" + dir);
        } else {
            File file = new File(path.getFile());
            File[] tempList = file.listFiles();
            if (tempList != null && tempList.length != 0) {
                for(int i = 0; i < tempList.length; ++i) {
                    File f = tempList[i];
                    if (f.isFile() && f.getName().indexOf("status-code") == 0) {
                        System.out.println("文件：" + f);
                        this.load(f);
                    }
                }

            } else {
                System.out.println("don't find any files");
            }
        }
    }

    public static String getValue(String key) {
        if (key != null && !"".equals(key)) {
            if (instance == null) {
                initInstance();
            }

            String value = properties.getProperty(key);
            return value;
        } else {
            return "";
        }
    }
}